package org.softmed.rest.editor.comps.browser;

import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import net.miginfocom.swing.MigLayout;

import org.softmed.rest.editor.EditorUtil;
import org.softmed.rest.editor.commons.SwingUtil;
import org.softmed.swing.IconManager;

public class NavBar extends JPanel {

	JButton next = new JButton(IconManager.goNext);
	JButton previous = new JButton(IconManager.goPrevious);
	JButton first = new JButton(IconManager.goFirst);
	JButton last = new JButton(IconManager.goLast);
	// JLabel currentPageNumber = new JLabel();

	JComboBox pages = new JComboBox();

	// int width = 20;
	// int height = 20;

	public NavBar() {

		next.setToolTipText("Show next page");
		previous.setToolTipText("Show previous page");
		first.setToolTipText("Show first page");
		last.setToolTipText("Show last page");

		pages.setToolTipText("Select page to display");

		setButtonBorder(BorderFactory.createLineBorder(Color.GRAY));

		// setBackground(Color.WHITE);
		// SwingUtil.setSize(this, EditorUtil.restBrowserWidth, 20);
		MigLayout layout = new MigLayout("left,insets 0, gap 0");
		setLayout(layout);

		// first.setAlignmentY(TOP_ALIGNMENT);
		pages.setBorder(BorderFactory.createLineBorder(Color.GRAY));

		add(first, getMigStuff());
		add(previous, getMigStuff());
		add(pages, "w 50! , h " + EditorUtil.buttonHeightAndWidth + "!");
		add(next, getMigStuff());
		add(last, getMigStuff());

	}

	public void setButtonBorder(Border border) {
		next.setBorder(border);
		previous.setBorder(border);
		first.setBorder(border);
		last.setBorder(border);
	}

	public void addNavButtonsActionListener(ActionListener listener) {
		first.addActionListener(listener);
		previous.addActionListener(listener);
		next.addActionListener(listener);
		last.addActionListener(listener);
	}

	public String getMigStuff() {
		return "w " + (EditorUtil.buttonHeightAndWidth + 10) + "!, h "
				+ EditorUtil.buttonHeightAndWidth + "!";
	}

	public void setPagedList(PagedList plist) {

		// currentPageNumber.setText("" + plist.getCurrentPageNumber());

		disableButtons();

		if (plist.getFirstURI() != null)
			first.setEnabled(true);

		if (plist.getPreviousURI() != null)
			previous.setEnabled(true);

		if (plist.getNextURI() != null)
			next.setEnabled(true);

		if (plist.getLastURI() != null)
			last.setEnabled(true);

		int totalPages = 0;
		if (plist.getItemsPerPage() != 0) {
			totalPages = (int) (plist.getItemCount() / plist.getItemsPerPage());
			int rest = (int) (plist.getItemCount() % plist.getItemsPerPage());
			if (rest > 0)
				totalPages++;

		}

		// this.totalPages.setText("" + totalPages);
		pages.removeAllItems();

		for (int i = 1; i <= totalPages; i++) {
			pages.addItem(i);
		}

		long currentPage = plist.getCurrentPageNumber();
		currentPage = (int) currentPage - 1;
		if (currentPage == 0)
			currentPage = -1;
		pages.setSelectedIndex((int) currentPage);

	}

	public void disableButtons() {
		first.setEnabled(false);
		previous.setEnabled(false);
		next.setEnabled(false);
		last.setEnabled(false);
	}

	public JButton getNext() {
		return next;
	}

	public JButton getPrevious() {
		return previous;
	}

	public JButton getFirst() {
		return first;
	}

	public JButton getLast() {
		return last;
	}

	public JComboBox getPages() {
		return pages;
	}

}
